<?php
namespace Question\Controller;
use Common\Controller\AdminBase;
class QuestionController extends AdminBase {
    //招聘模型对象
    private $Question = NULL;
    //应聘模型对象
    private $Candidates = NULL;
    protected function _initialize() {
        parent::_initialize();
        $this->Question = D('Question/Question');
        $this->Candidates = D('Candidates/questions');
    }
    //后台管理首页
    public function index() {
        if (IS_POST) {
            //排序
            $id = I('post.id');
            $listorder = I('post.listorder');
            if (empty($id)) {
                $this->error('请选择需要排序的信息！');
            }
            foreach ($id as $k => $id) {
                $this->Question->where(array('id' => $id))->save(array('listorder' => $listorder[$k]));
            }
            $this->success('排序更新成功！', U('Question/index'));
            exit;
        }
        //条件
        $where = array();
        $keyword = I('get.keyword');
        if ($keyword) {
            $where['zhiwei'] = array('LIKE', "%{$keyword}%");
            $this->assign('keyword', $keyword);
        }

        $count = $this->Question->where($where)->count();
        $page = $this->page($count, 20);
        $data = $this->Question->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("id" => "DESC"))->select();
        //统计
        $online = $this->Question->where(array('status' => 1, 'enddate' => array('GT', time())))->count();
        $candidates = $this->Candidates->count();

        $this->assign('data', $data);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->assign('online', $online);
        $this->assign('candidates', $candidates);
        $this->display();
    }
    public function edit() {
        if (IS_POST) {
            $post = $_POST;
            if ($this->Question->editQuestion($post)) {
                $this->success('答复成功！', U('Question/index'));
            } else {
                $error = $this->Question->getError();
                $this->error($error ? $error : '回复失败！');
            }
        } else {
            $questionId = I('get.id', 0, 'intval');
            if (empty($questionId)) {
                $this->error('请选择需要答复的问题！');
            }
            $info = $this->Question->getInfo($questionId);
            switch($info['kemu']){
                case '1':
                    $info['kemu']="物理";
                    break;
                case '2':
                    $info['kemu']="化学";
                    break;
                case '3':
                    $info['kemu']="语文";
                    break;
                case '4':
                    $info['kemu']="数学";
                    break;
                case '5':
                    $info['kemu']="生物";
                    break;
                case '6':
                    $info['kemu']="地理";
                    break;
                case '7':
                    $info['kemu']="政治";
                    break;
                case '8':
                    $info['kemu']="历史";
                    break;
                default :
                    $info['kemu']="未知科目";
            }
            if (empty($info)) {
                $this->error('你选择需要答复的问题不存在！');
            }
            $this->assign('info', $info);
            $this->display();
        }
    }
    public function delete() {
        if (IS_POST) {
            $Id = I('post.id');
        } else {
            $Id = I('get.id', 0, 'intval');
        }
        if (empty($Id)) {
            $this->error('请选择需要删除的留言信息！！！');
        }
        return  $this->Question->delete($Id);
        if ($this->Question->delete($Id)) {
            $this->success('删除成功！！');
        } else {
            $this->error('删除信息失败！！');
        }
    }

    //应聘简历管理
    public function questions() {
        //条件
        $where = array();
        $keyword = I('get.keyword');
        if ($keyword) {
            $where['name'] = array('LIKE', "%{$keyword}%");
            $this->assign('keyword', $keyword);
        }
        $count = $this->Question->where($where)->count();
        $page = $this->page($count, 10);
        $data = $this->Question->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("id" => "DESC"))->select();
        $data = $data ? $data : array();
        foreach ($data as &$val){
            switch($val['kemu']){
                case '1':
                    $val['kemu']="物理";
                    break;
                case '2':
                    $val['kemu']="化学";
                    break;
                case '3':
                    $val['kemu']="语文";
                    break;
                case '4':
                    $val['kemu']="数学";
                    break;
                case '5':
                    $val['kemu']="生物";
                    break;
                case '6':
                    $val['kemu']="地理";
                    break;
                case '7':
                    $val['kemu']="政治";
                    break;
                case '8':
                    $val['kemu']="历史";
                    break;
                default :
                    $val['kemu']="未知科目";
            }
          
        }
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->assign('data', $data);
        $this->assign("Page", $page->show('Admin'));
        $this->display();
    }

    //删除应聘简历
    public function deleteyp() {
        if (IS_POST) {
            $id = I('post.ids');
        } else {
            $id = I('get.id', 0, 'intval');
        }
        if (empty($id)) {
            $this->error('请选择需要删除的应聘简历！');
        }
        if ($this->Candidates->deleteCandidates($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

}
